
//
//  VideoView.m
//  Slide
//
//  Created by jarze on 15/7/21.
//  Copyright (c) 2015年 jarze. All rights reserved.
//

#import "VideoView.h"
#import "MBProgressHUD+MJ.h"
#import "HMVideoCell.h"
#import "HMVideo.h"
#import <MediaPlayer/MediaPlayer.h>

@interface VideoView ()


@end


@implementation VideoView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    

    // 去除分割线
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    
//    [MBProgressHUD showMessage:@"正在加载视频信息...."];
    
    //    NSXMLParser  XML 解析
    // MediaPlayer\AVFoundation
    
    // 访问服务器数据
    NSString *urlStr = @"http://192.168.1.18:8080/MJServer/video";
    
    // 发送请求
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // GET
    request.timeoutInterval = 10;
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        // 隐藏
//        [MBProgressHUD hideHUD];
        
        if (data) {
            // 解析数据
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSArray *array = dict[@"videos"];
            
            NSMutableArray *videos = [NSMutableArray array];
            for (NSDictionary *videoDict in array) {
                HMVideo *video = [HMVideo videoWithDict:videoDict];
                [videos addObject:video];
            }
            self.videos = videos;
            
            // 刷新表格
            [self reloadData];
        } else {
            [MBProgressHUD showError:@"网络繁忙!!!"];
        }
    }];
    }
    return self;
}

//
//#pragma mark - 数据源
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return self.videos.count;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    HMVideoCell *cell = [HMVideoCell cellWithTableView:tableView];
//    
//    cell.video = self.videos[indexPath.row];
//    
//    return cell;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 70;
//}
//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    HMVideo *video = self.videos[indexPath.row];
//    
//    // 播放视频
//    NSString *videoUrl = [NSString stringWithFormat:@"http://192.168.1.18:8080/MJServer/%@", video.url];
//    
//    //   NSString *videoUrl = [[NSBundle mainBundle] pathForResource:@"minion_01.mp4" ofType:nil];
//
//    MPMoviePlayerViewController *playerVc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:videoUrl]];
//    
//    [self.superclass presentMoviePlayerViewControllerAnimated:playerVc];
//    
//    // 只能全屏播放
//    //    MPMoviePlayerController; // 可以随意控制播放器的尺寸
//}




@end
